package com.bluegalaxy.sitescore.controller;

import com.bluegalaxy.sitescore.entity.*;
import com.bluegalaxy.sitescore.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/23 0023.
 */
@Controller
@RequestMapping("/siteSource")
public class SiteSourceController extends BaseController {

    @Autowired
    private TbSiteSourceService siteSourceService;

    @Autowired
    private ITbDangerousService dangerousService;

    @Autowired
    private ITBSelectDangerousService selectDangerousService;

    @Autowired
    private IGroundWaterService groundWaterService;

    @Autowired
    private SurfaceWaterService surfaceWaterService;

    @Autowired
    private SurfaceWaterGWService surfaceWaterGWService;

    @Autowired
    private ITbSoilExposureService soilExposureService;

    @Autowired
    private ITbAirScoreService airScoreService;

    @Autowired
    private ITbSiteInfoService siteInfoService;

    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/view/{siteId}")
    public String view(@PathVariable Integer siteId,Model model) {
        model.addAttribute("siteId", siteId);
        BigDecimal groundWater = null;
        BigDecimal surfaceWater = null;
        Float soil = null;
        Float air = null;
        BigDecimal score = null;
        TbSiteInfo tbSiteInfo = siteInfoService.getSiteInfoById(Long.valueOf(siteId));
        List<TbScore> tbGroundWater = groundWaterService.getGroundWaterList(siteId);
        List<TbScore> tbSurfaceWater = surfaceWaterService.getSWOverLandList(siteId);
        List<TbScore> tbSurfaceWaterGround = surfaceWaterGWService.getSWGroundList(siteId);
        TbSoilExposure tbSoilExposure = soilExposureService.selectById(siteId);
        TbAirScore tbAirScore = airScoreService.getAirBySid(siteId);
        if(!tbGroundWater.isEmpty()){
            groundWater = tbGroundWater.get(0).getScore();
        }
        if(!tbSurfaceWater.isEmpty()&&!tbSurfaceWaterGround.isEmpty()){
            BigDecimal ss = tbSurfaceWater.get(0).getScore();
            BigDecimal sg = tbSurfaceWaterGround.get(0).getScore();
            surfaceWater = ss.compareTo(sg)==1?ss:sg;
            if(surfaceWater.compareTo(BigDecimal.valueOf(100))==1){
                surfaceWater = BigDecimal.valueOf(100);
            }
        }
        if(tbSoilExposure!=null){
            soil = tbSoilExposure.getSoil_exp_score();
        }
        if(tbAirScore !=null){
            air = tbAirScore.getAir_pathway_score();
        }
        if (groundWater!=null&&surfaceWater!=null&&soil!=null&&air!=null){
            score = siteSourceService.caculateScore(groundWater,surfaceWater,BigDecimal.valueOf(soil),BigDecimal.valueOf(air));
            siteInfoService.updateSiteInfoScore(siteId.intValue(),score);
        }
//        BigDecimal score = siteSourceService.caculateScore(BigDecimal.valueOf(15.04),BigDecimal.valueOf(26.93),BigDecimal.valueOf(41.66),BigDecimal.valueOf(19.47));
        model.addAttribute("groundWater",groundWater);
        model.addAttribute("surfaceWater",surfaceWater);
        model.addAttribute("soil",soil);
        model.addAttribute("air",air);
        model.addAttribute("score",score);
      //  model.addAttribute("siteSource", siteSourceService.getTbSiteSource(siteId));
        return "/score/view";
    }

    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/add/{siteId}/{id}")
    public String add(@PathVariable Integer siteId,@PathVariable Integer id,Model model) {
        TbSiteSource tbSiteSource = new TbSiteSource();
        List<SiteSource> siteSourceList = siteSourceService.getTbSiteSource(siteId);
        if(siteSourceList.isEmpty()){
            model.addAttribute("source",tbSiteSource);
        }else {
            tbSiteSource = siteSourceService.getTbSiteSourceById(siteSourceList.get(0).getId());
            model.addAttribute("source",tbSiteSource);
        }
       /* if(id==0){
            model.addAttribute("source",tbSiteSource);
        }else{
            tbSiteSource = siteSourceService.getTbSiteSourceById(id);
           // System.out.println(tbSiteSource.getSource_type());
            model.addAttribute("source",tbSiteSource);
        }*/
        model.addAttribute("dangerousList",toJson(dangerousService.getDangerousAll()));
        model.addAttribute("siteId", siteId);
        model.addAttribute("dangerousSelect",toJson(selectDangerousService.getDangerousBysid(siteId,1)));
      //  model.addAttribute("siteSource", toJson(siteSourceService.getTbSiteSource(siteId)));
        return "/source/add";
    }

    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public @ResponseBody String save(TSiteSource siteSource,Model model) {
        int id =Integer.valueOf(siteSource.getSiteId());
        int[] dangerous = siteSource.getDangerous();
        //获取旧毒素
       /* List<SelectDangerous> selectDangerousListOld = selectDangerousService.getDangerousBysid(id,-1);
        List<Integer> idList = new ArrayList<>();
        //存旧毒素ID
        for(SelectDangerous dangerous1:selectDangerousListOld){
            idList.add(dangerous1.getDangerous_id());
        }*/
        List<SelectDangerous> selectDangerousList = new ArrayList<>();
        //加入选择毒素集合
        for (int i : dangerous){
            SelectDangerous selectDangerous = new SelectDangerous();
            selectDangerous.setType(1);
            selectDangerous.setSource_id(id);
            selectDangerous.setDangerous_id(i);
            selectDangerousList.add(selectDangerous);
            //旧毒素包含此id
           /* if(idList.contains(i)){
                //将原记录添加回去
                for (SelectDangerous dangerous1:selectDangerousListOld){
                    if (dangerous1.getDangerous_id()==i&&dangerous1.getType()!=1){
                        selectDangerousList.add(dangerous1);
                    }
                }
            }*/

        }
        if(selectDangerousService.delDangerousListBySid(id)){
            if(!selectDangerousService.insertDangerousList2(selectDangerousList)){
                return "false";
            }
        }else {
            if(!selectDangerousService.insertDangerousList2(selectDangerousList)){
                return "false";
            }
        }
       if( siteSourceService.caculateHWQ(siteSource)){
           return "success";
       }
        return "false";
    }

    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping(value = "/delSource",method = RequestMethod.POST)
    public @ResponseBody
    String GroundDel(int id, Model model){
        if (siteSourceService.delTbSiteSource(id)){
            return "{\"success\":\"删除成功\"}";
        }else {
            return "{\"false\":\"删除异常\"}";
        }
    }



}
